using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;
using System.Collections;

namespace Quanly_dethi.Class
{
    class csC_Admin
    {
        private ketnoi kn;

        public ketnoi Kn
        {
            get { return kn; }
            set { kn = value; }
        }
        private string databasename;

        public string Databasename
        {
            get { return databasename; }
            set { databasename = value; }
        }
        public csC_Admin()
        {
            Kn = new ketnoi();
            databasename = "";
        }
        public csC_Admin(string servername, string user, string pass, string dbname)
        {
            Kn = new ketnoi(servername ,user, pass);
            databasename = dbname;
        }
        public void xuly(string ten)
        {
            Database db = Kn._Server.Databases[databasename];
            string s = "BACKUP DATABASE [TTN] TO  DISK = N" + "'" + ten + "'" + "WITH NOFORMAT, NOINIT,  NAME = N" + "'" + ten + "'" + ", SKIP, NOREWIND, NOUNLOAD,  STATS = 10";
            db.ExecuteWithResults(s);
        }
        public bool xuly_phuchoi(string duongdan)
        {
            Database db = Kn._Server.Databases[databasename];
            string s = "RESTORE DATABASE [TTN] FROM  DISK = N" + "'" + duongdan + "'" + "WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 10";
            try
            {
                db.ExecuteWithResults(s);
                return true;
            }
            catch 
            {
                return false;
            }
            
        }
        public Boolean select(csE_Admin u)
        {

            Database db = Kn._Server.Databases[databasename];
            string s = "select TenND,Matkhau from Nguoidung where Matkhau='" + u.Matkhau + "' and TenDN='" + u.Tennguoidung + "'";
            DataTable k = db.ExecuteWithResults(s).Tables[0];
            if (k.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        public DataTable selectall()
        {
            Database db = Kn._Server.Databases[databasename];
            return db.ExecuteWithResults("select MaND,TenND,Matkhau,TenQuyen from Nguoidung nd,Quyen Q where nd.MaQuyen=Q.MaQuyen").Tables[0];
           
        }
        public void insert(csE_Admin x)
        {
            Database db = Kn._Server.Databases[databasename];
            string s = "insert into Nguoidung(TenND, MaQuyen,Matkhau,Xoa) values('" + x.Tennguoidung.ToString() + "','" + int.Parse(x.Maquyen.ToString()) + "','" + x.Matkhau.ToString() + "','" + int.Parse(x.Xoa.ToString()) + "')";

            db.ExecuteWithResults(s);

        }
        public void Update(csE_Admin x)
        {
            string t = "update NguoiDung set TenND=N'" + x.Tennguoidung + "', MaQuyen='" + x.Maquyen + "', Matkhau='" + x.Matkhau + "', Xoa='0' where MaND='" + x.Manguoidung + "'";
            Database db = Kn._Server.Databases[databasename];
            db.ExecuteWithResults(t);

        }
    }
}
